(function(window) {
	var vm, pullRefresh;
	var devSN = "";
	var userName = "";
	var pageSize = 8;
	var regionNameOfClickedShop = "";
	var commonLocale = dataManager.getLocale();

	function pulldownRefresh(isPull) {
		isPull = isPull === undefined ? true : isPull;
		getShopList(userName, 1, pageSize, function() {
			vm.shopInfo = {};
			vm.shopCount = 0;
			isPull && pullRefresh.endPullDownToRefresh();
			isPull && pullRefresh.refresh(true);
		});
	}

	function pullupRefresh() {
		var page = Math.ceil(vm.shopCount / pageSize) + 1;
		getShopList(userName, page, pageSize, function(totalCount) {
			pullRefresh && pullRefresh.endPullUpToRefresh(totalCount && totalCount === vm.shopCount);
		});
	}

	function getShopList(userName, page, pageSize, callback) {
		v3Restshop.getShopListOfUser(userName, '', page, pageSize, function(code, data) {
			if(code === 0 && data) {
				callback(data.rowCount);
				var shopIds = [];
				if(data.data) {
					vm.shopCount += data.data.length;
					for(var i = 0; i < data.data.length; i++) {
						var shop = data.data[i];
						if(!vm.shopInfo[shop.regionName]) {
							vm.$set(vm.shopInfo, shop.regionName, []);
						}
						vm.shopInfo[shop.regionName].push({
							shopName: shop.shopName,
							shopId: shop.id,
							scenarioName: shop.scenarioName,
							scenarioId: shop.scenarioId,
							devSN: shop.sn,
							shopHealth: vm.$t('shopStatus[2]'), //"未知",
							shopStatus: 3, //0 无设备 1在线 2离线 3获取失败
							customType: shop.customType
						});

						shopIds.push(shop.id);
					}
				}
				getShopHealth(shopIds);
			} else if(code === 1) {
				callback();
				commonTool.toast(data);
			} else {
				callback();
			}
		});
	}

	function getShopHealth(shopIds) {
		if(shopIds.length > 0) {
			v3AntService.getHealthOfShops(shopIds, function(code, data) {
				console.log(shopIds);
				console.log(JSON.stringify(data));
				if(code === 0) {
					$.each(data, function(i, healthInfo) {
						var regionNames = Object.keys(vm.shopInfo);
						for(var j = 0; j < regionNames.length; j++) {
							var shopsInRegion = vm.shopInfo[regionNames[j]];
							var hasFinded = false;
							for(k = 0; k < shopsInRegion.length; k++) {
								if(healthInfo.scenarioId == shopsInRegion[k].shopId) {
									if(healthInfo.message == "offline") {
										shopsInRegion[k].shopHealth = vm.$t('shopStatus[1]'); //"离线";
										shopsInRegion[k].shopStatus = 2;
									} else if(healthInfo.message == "success") {
										if(healthInfo.info.score){
											shopsInRegion[k].shopHealth = healthInfo.info.score + "分";
										} else {
											shopsInRegion[k].shopHealth = "0分";
										}
										
										shopsInRegion[k].shopStatus = 1;
									} else if(healthInfo.message == "noDevice") {
										shopsInRegion[k].shopHealth = vm.$t('shopStatus[0]'); //"无设备";
										shopsInRegion[k].shopStatus = 0;
									} else {
										shopsInRegion[k].shopHealth = vm.$t('shopStatus[2]'); //"未知";
										shopsInRegion[k].shopStatus = 3;
									}
									hasFinded = true;
									break;
								}
							}
							if(hasFinded) {
								break;
							}
						}
					});
				}
			});
		}
	}

	function cellClicked(shop, regionName) {
		if(shop.devSN === "") {
			regionNameOfClickedShop = regionName;
			var btnArray = [vm.$t('cancel'), vm.$t('add')];
			mui.confirm(vm.$t('noDevice'), vm.$t('hint'), btnArray, function(e) {
				if(e.index == 1) {
					//console.log("add");
					commonTool.openWindow('scenarios/common/device/adddevice.html', "adddevice.html", {
						shopId: shop.shopId
					});
				} else {
					//console.log("cancel");
				}
			});
			return;
		}
		//var type = getScenarioTypeByScenarioName(shop.scenarioName);

//		if(type != "xiaoxiaobei") {
//			type = "business";
//		}
		var scenarioTypeStr = "";
		if(shop.customType === 2){
			//小小贝场景
			scenarioTypeStr = "xiaoxiaobei";
		}else if(shop.customType === 7){
			//中小企业-安全网络
			scenarioTypeStr = "security";
		}else{
			scenarioTypeStr = "new-business";
			//scenarioTypeStr = "business";
		}

		$.util.setShopId(shop.shopId);
		$.util.setShopInfo(shop);
		dataManager.setCurrentScenarioType(scenarioTypeStr);
		var url = 'scenarios/' + scenarioTypeStr + '/index.html';
		commonTool.openWindow(url, scenarioTypeStr + '/index.html', {
			shopId: shop.shopId,
			customType: shop.customType
		});
	}

//	function getScenarioTypeByScenarioName(scenarioName) {
//		var type = "";
//		if(scenarioName.indexOf(commonLocale.typeName.business) >= 0) {
//			type = "business";
//		} else if(scenarioName.indexOf(commonLocale.typeName.xiaoxiaobei) >= 0) {
//			type = "xiaoxiaobei";
//		} else if(scenarioName.indexOf(commonLocale.typeName.zone) >= 0) {
//			type = "zone";
//		} else if(scenarioName.indexOf(commonLocale.typeName.classroom) >= 0) {
//			type = "class";
//		} else if(scenarioName.indexOf(commonLocale.typeName.businessParks) >= 0) {
//			type = "business-parks";
//		} else {
//			type = "unknown";
//		}
//		return type;
//	}

	function getScenarioImgByScenarioType(scenarioType) {
		var result = "";
		//console.log("scenarioType:"+scenarioType);
		if(scenarioType === 1){
			//园区wifi
			result = "images/scenario-park-icon.png";
		}else if(scenarioType === 2){
			//商铺wifi
			result = "images/scenario-shop-icon.png";
		}else if(scenarioType === 3){
			//中小企业-智慧网络
			result = "images/scenario-classroom-icon.png";
		}else if(scenarioType === 4){
			//园区商业wifi
			result = "images/scenario-zone-icon.png";
		}else if(scenarioType === 5){
			//中小企业-智慧无线
			result = "images/scenario-classroom-icon.png";
		}else if(scenarioType === 6){
			//v5中小企业
			result = "images/scenario-business-icon.png";
		}else if(scenarioType === 7){
			//中小企业-安全网络
			result = "images/scenario-business-icon.png";
		}else if(scenarioType === 9){
			//FAT AP
			result = "images/scenario-business-icon.png";
		}else{
			result = "images/scenario-business-icon.png";
		}
		return result;
//		var scenarioType = getScenarioTypeByScenarioName(scenarioName);
//		if(scenarioType === "business") {
//			result = "images/scenario-business-icon.png";
//		} else if(scenarioType === "xiaoxiaobei") {
//			result = "images/scenario-shop-icon.png";
//		} else if(scenarioType === "zone") {
//			result = "images/scenario-zone-icon.png";
//		} else if(scenarioType === "class") {
//			result = "images/scenario-classroom-icon.png";
//		} else if(scenarioType === "business-parks") {
//			result = "images/scenario-park-icon.png";
//		} else {
//			result = "images/scenario-business-icon.png";
//		}
//		return result;
	}

	function startSlider() {
		var slider = mui("#scenario_slider");
		slider.slider({
			interval: 5000
		});
	}

	function deleteShopByShopId(shopId, regionName, event) {
		var btnArray = [vm.$t('base.cancel'), vm.$t('base.confirm')]; //['取消', '确认'];
		mui.confirm(vm.$t('confirmDeleteShop'), vm.$t('hint'), btnArray, function(e) {
			if(e.index == 1) {
				v3Restshop.deleteShopByShopId(userName, shopId, function(code, data) {
					if(code === 0) {
						$.each(vm.shopInfo[regionName], function(i, shop) {
							if(shop.shopId === shopId) {
								vm.shopInfo[regionName].splice(i, 1);
								vm.shopCount--;
								return false;
							}
						});
						var li = event.target.parentNode.parentNode;
						mui.swipeoutClose(li);
					} else if(code === 1) {
						commonTool.toast(data);
					}
				});
			}
		});

	}

	window.addEventListener('addDeviceToShop', function(event) {
		if(regionNameOfClickedShop && vm.shopInfo[regionNameOfClickedShop]) {
			$.each(vm.shopInfo[regionNameOfClickedShop], function(i, shop) {
				if(shop.shopId === event.detail.shopId) {
					shop.devSN = event.detail.devSN;
					shop.shopStatus = 3;
					shop.shopHealth = vm.$t('shopStatus[2]');

					//获取场所健康度
					var shopIds = [shop.shopId];
					getShopHealth(shopIds);
					return false;
				}
			});
		}
	});

	window.addEventListener("addNewShop", function(event) {
		vm.shopCount++;
		if(!vm.shopInfo[event.detail.regionName]) {
			vm.$set(vm.shopInfo, event.detail.regionName, []);
		}
		vm.shopInfo[event.detail.regionName].push({
			shopName: event.detail.shopName,
			shopId: event.detail.shopId,
			scenarioName: event.detail.scenarioName,
			scenarioId: event.detail.scenarioId,
			devSN: event.detail.devSN,
			shopHealth: vm.$t('shopStatus[0]'), //"无设备",
			shopStatus: 0,
			customType: event.detail.customType    //to do 添加新场所时需要发送这个参数
		});
	});

	function mounted() {
		vm = this;
		pullRefresh = $('#pullrefresh').pullToRefresh({
			down: {
				callback: pulldownRefresh
			},
			up: {
				contentinit: vm.$t('base.pullUpTips'),
				contentdown: vm.$t('base.pullUpTips'),
				contentrefresh: vm.$t('base.loading'),
				contentnomore: vm.$t('base.noMore'),
				callback: pullupRefresh
			}
		});
		$.plusReady(function() {
			devSN = $.util.getDevSN();
			userName = dataManager.getUserName();
			pulldownRefresh(false);
			startSlider();
		});
	}

	window.shop = {
		mounted: mounted,
		data: {
			pictures: [
				'images/scenario-zone.png',
				'images/scenario-business.png',
				'images/scenario-classroom.png',
				'images/scenario-park.png',
				'images/scenario-shop.png',
				'images/scenario-zone.png',
				'images/scenario-business.png'
			],
			shopInfo: {},
			shopCount: 0
		},
		methods: {
			cellClicked: cellClicked,
			getScenarioImgByScenarioType: getScenarioImgByScenarioType,
			deleteShopByShopId: deleteShopByShopId
		}
	};
})(window);